import os
import sys
import subprocess
import optparse
import glob
import string

import arg_check

def pcap_test(path,tshark_path):
    os.chdir(path)
    for pcap_file in glob.glob("*.pcap"):
        print "Checking for %s file.. " % pcap_file
        name_parts=pcap_file.split('.')
        if os.path.exists(name_parts[0]+".txt"):
            print "Test case file exists for %s. Checking the testcases. " \
% pcap_file
            fd=0
            try:
                fd=open(name_parts[0]+".txt","r")
                lines=fd.readlines()
                num_received=0
                if len(lines)==0:
                    print "No testcases defined for %s in file %s. Skipping \
it." % (pcap_file,name_parts[0]+".txt")
                    print "-------------------------------------------------\
-------------------"
                    continue
                for line in lines:
                    ind_part=line.split(':')
                    output=subprocess.Popen([tshark_path,"-r",
                                             pcap_file,
                                             "-R",
                                             ind_part[1]],
                                            stdout=subprocess.PIPE)
                    output.wait()
                    lines = output.stdout.readlines()
                    num_received = len(lines)
                    if num_received==int(ind_part[2]):
                        print "Test case \"%s\" passed." % ind_part[1]
                    else:
                        print "Test case \"%s\" failed." % ind_part[1]
                fd.close()
                print "---------------------------------------------------\
-----------------"
            except Exception, e:
                print e
                print "Error occurred in creating output file for tshark \
test for %s pcap file. Program exiting now...." % pcap_file
                return -4000
        else:
            print "Not found......"
    
if __name__=="__main__":
    x=0
    parser=optparse.OptionParser()
    parser.add_option("-p","--path",
                      dest="path",
                      type="string",
                      help="This takes path of Pcap_files directory\
 which contains the pcap and testcase files....",
                      action="store")

    parser.add_option("-t","--tshark",
                      dest="tshark",
                      type="string",
                      help="This takes path of tshark \
which is generated in compilation of wireshark code.....",
                      action="store")
    
    (options, args)=parser.parse_args()
    path=options.path
    tshark_path=options.tshark
    path_exists=arg_check.path_check(path)
    if path_exists==False:
        print "Wrong path %s specified...... Please check the first \
argument......" % path
        print "Program exiting now........"
        exit(1)
    path_exists=arg_check.path_check(tshark_path)
    if path_exists==False:
        print "Wrong path %s specified...... Please check the first \
argument......" % tshark_path
        print "Program exiting now........"
        exit(1)
    pcap_test(path,tshark_path)
    print "DONE."
